﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MovmentDAL : IMovmentDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MovmentDAL()
        {

        }
        public IList<MovmentInfo> ListMovments(MovmentInfo movInfo)
        {
            string strSQL;
            int EmpType = 0;
            if (movInfo.EmployeeType != null)
                EmpType = movInfo.EmployeeType.Sequence;

            List<MovmentInfo> infoList = new List<MovmentInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@Sequence", movInfo.Sequence);
                dbManager.AddParameters(1, "@EmpType", EmpType);
                dbManager.AddParameters(2, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT MOVE_SEQ, EMP_TYPE_SEQ, MOVE_CODE, MOVE_NAME, MOVE_TYPE"
                        + ", REMARKS, RECORD_STATUS, CREATED_BY, UPDATED_BY"
                    + " FROM HRM_PS_EMP_MOVMENTS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (movInfo.Sequence > 0 ? " AND MOVE_SEQ = @Sequence" : "")
                    + (EmpType > 0 ? " AND EMP_TYPE_SEQ = @EmpType" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    EmployeeTypeDAL emptypeDAL = new EmployeeTypeDAL();
                    EmployeeTypeInfo etInfo = new EmployeeTypeInfo();
                    if (!rdr.IsDBNull(1))
                    {
                        IList<EmployeeTypeInfo> emptypeList = emptypeDAL.ListEmployeeTypes(new EmployeeTypeInfo(rdr.GetInt32(1)));
                        etInfo = emptypeList[0];
                    }

                    MovmentInfo Info = new MovmentInfo(rdr.GetInt32(0)
                        , etInfo
                        , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                        , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                        , rdr.IsDBNull(4) ? "" : rdr.GetString(4)
                        , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                        , rdr.IsDBNull(6) ? "" : rdr.GetString(6)
                        , rdr.IsDBNull(7) ? (int?)null : rdr.GetInt32(7)
                        , rdr.IsDBNull(8) ? (int?)null : rdr.GetInt32(8)
                        );

                    infoList.Add(Info);
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return infoList;
        }
        public void Insert(MovmentInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@EmpTypeSeq", Info.EmployeeType == null ? (int?)null : Info.EmployeeType.Sequence);
                dbManager.AddParameters(1, "@Code", Info.Code);
                dbManager.AddParameters(2, "@Name", Info.Name);
                dbManager.AddParameters(3, "@Type", Info.Type);
                dbManager.AddParameters(4, "@Remarks", Info.Remarks);
                dbManager.AddParameters(5, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(6, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(7, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_PS_EMP_MOVMENTS(EMP_TYPE_SEQ, MOVE_CODE, MOVE_NAME, MOVE_TYPE"
                         + ", REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@EmpTypeSeq, @Code, @Name, @Type, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(MovmentInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@Sequence", Info.Sequence);
                dbManager.AddParameters(1, "@EmpTypeSeq", Info.EmployeeType == null ? (int?)null : Info.EmployeeType.Sequence);
                dbManager.AddParameters(2, "@Code", Info.Code);
                dbManager.AddParameters(3, "@Name", Info.Name);
                dbManager.AddParameters(4, "@Type", Info.Type);
                dbManager.AddParameters(5, "@Remarks", Info.Remarks);
                dbManager.AddParameters(6, "@RecordStatus", Info.RecordStatus);
                dbManager.AddParameters(7, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(8, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_EMP_MOVMENTS "
                        + " SET EMP_TYPE_SEQ = @EmpTypeSeq"
                        + ", MOVE_CODE = @Code"
                        + ", MOVE_NAME = @Name"
                        + ", MOVE_TYPE = @Type"
                        + ", REMARKS = @Remarks"
                        + ", RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE MOVE_SEQ = @Sequence";                

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_EMP_MOVMENTS "
                        + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE MOVE_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
